package com.youlu.campus.web.card;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.card.dto.CardCateQueryDTO;
import com.youlu.campus.entity.card.dto.CardOrderCreateDTO;
import com.youlu.campus.entity.card.dto.CardOrderQueryDTO;
import com.youlu.campus.service.card.TsCardService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员卡h5卡控制器
 * 处理会员卡相关的查询、订单创建及订单列表查询等操作
 *
 * @author : [dengbo]
 * @createTime : [2022/12/17 16:28]
 */
@Slf4j
@RestController
@RequestMapping("/mini-app/")
public class TsCardController {
    @Value("${sys.pay.wx.payWechatNotifyUrl}")
    private String notifyUrl;

    @Autowired
    private TsCardService tsCardService;

    /**
     * 会员卡首页列表接口
     * @return 会员卡首页列表数据
     */
    @PostMapping("/card/list")
    public QResult getFistCateList() {
        return QResult.success(tsCardService.getFistCateList());
    }

    /**
     * 会员卡详情查询
     * @param request 会员卡查询条件DTO
     * @return 会员卡详情信息
     */
    @PostMapping("/card/detail")
    public QResult getCateDetail(@Validated @RequestBody CardCateQueryDTO request) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(request));
        return QResult.success(tsCardService.getFistCateDetail(request));
    }

    /**
     * 会员卡下单
     * @param request 订单创建DTO
     * @param httpRequest HTTP请求对象，用于获取客户端IP
     * @return 订单创建结果
     */
    @PostMapping("/card/create/order")
    public QResult createOrder(@Validated @RequestBody CardOrderCreateDTO request, HttpServletRequest httpRequest) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setUserPO(RequestContext.getCurUser());
        request.setClientIp(CommonUtils.getRemoteIp(httpRequest));
        request.setNotifyUrl(notifyUrl);
        return QResult.success(tsCardService.createTsOrder(request));
    }

    /**
     * 会员卡片订单列表查询
     * @param dto 订单查询DTO
     * @return 订单列表数据
     */
    @PostMapping("/card/order/list")
    public QResult getOrderList(@Validated @RequestBody CardOrderQueryDTO dto) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(dto));
        dto.setAppId(RequestContext.getAppId());
        dto.setUserId(userId);
        return QResult.success(tsCardService.getOrderList(dto));
    }

}